<?php 
class X_ajaxmvc extends X_mvc{
    public function __construct($indexFile="index.php",$defaultController="index",$defaultAction="index",$defaultRoute="/:controller/:action/"){
        X_options::$ajaxmvc= true;
        X_config::$mvc_defaultViewClass= "X_ajaxview";
        parent::__construct();
    }
    
}

class X_ajaxview extends X_view{
    protected $javascript="";
    protected function uri($url){
        $isSearchEngine = (bool)(
            strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot") || 
            strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "bingbot")   ||
            strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "google page speed insights")
        );
        if($isSearchEngine){
            return $url;
        }
        return "javascript:x_ajaxurl('".$url."');void(0);";
    }
    
    public function appendScript($script){
        if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
            $this->javascript .= $script."\n";
        }else{
            parent::appendScript($script);
        }
    }
    
    public function renderLayout($layout){
        if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
            echo $this->render();
            echo "<&javascript&>" . $this->javascript . "document.title='".addslashes($this->pageTitle)."';";
        }else{
            parent::renderLayout($layout);
        }
    }
}

include 'ajax.php';
?>
<script type="text/javascript"><!-- 
    var x_defaultBodyElement= 'content';
    function x_ajaxurl(url){
        x_ajaxLoadPage(url,true);
    }
    function x_ajaxLoad(url,params,container,callback){ // container=0 to prevent putting in any container
        if(container===undefined || container===null)container= x_defaultBodyElement;
        if(callback){
            x_ajax(url, params, function(data){
                if(container){
                    x_ajaxEval(data,container);
                    x_callback(callback,[data]);
                }else{
                    return data;
                }
            }, true);
        }else{
            var data= x_ajax(url, params, null, true);
            if(container){
                x_ajaxEval(data,container);
            }else{
                return data;
            }
        }
    }
    x_ajaxHistoryEnabled = false;
    function x_ajaxLoadPage(url,changeUrl){
        if(window.history.pushState){
            x_ajaxLoad(url,null,null,function(data){
                if(changeUrl){
                    window.history.pushState([],null,url);
                    x_ajaxHistoryEnabled = true;
                }
            });
        }else{
            window.location= url;
        }
    }
    if(window.history.pushState && window.history.replaceState){
        window.onpopstate = function(e){
            if(x_ajaxHistoryEnabled){
                x_ajaxLoadPage(window.location.pathname || window.location || window.location.href,false);
            }
        }
    }
    // -->
</script>
